package com.zycfc.zsf.boot.util.net;

import com.google.common.net.*;
import java.net.*;
import com.zycfc.zsf.boot.util.number.*;
import com.zycfc.zsf.boot.util.text.*;
import java.util.*;

public abstract class IPUtils
{
    public static int toInt(final InetAddress address) {
        return InetAddresses.coerceToInteger(address);
    }
    
    public static String toString(final InetAddress address) {
        return InetAddresses.toAddrString(address);
    }
    
    public static Inet4Address fromInt(final int address) {
        return InetAddresses.fromInteger(address);
    }
    
    public static InetAddress fromIpString(final String address) {
        return InetAddresses.forString(address);
    }
    
    public static Inet4Address fromIpv4String(final String address) {
        final byte[] bytes = ip4StringToBytes(address);
        if (bytes == null) {
            return null;
        }
        try {
            return (Inet4Address)InetAddress.getByAddress(bytes);
        }
        catch (UnknownHostException e) {
            throw new AssertionError((Object)e);
        }
    }
    
    public static String intToIpv4String(final int i) {
        return new StringBuilder(15).append(i >> 24 & 0xFF).append('.').append(i >> 16 & 0xFF).append('.').append(i >> 8 & 0xFF).append('.').append(i & 0xFF).toString();
    }
    
    public static int ipv4StringToInt(final String ipv4Str) {
        final byte[] byteAddress = ip4StringToBytes(ipv4Str);
        if (byteAddress == null) {
            return 0;
        }
        return NumberMoreUtils.toInt(byteAddress);
    }
    
    private static byte[] ip4StringToBytes(final String ipv4Str) {
        if (ipv4Str == null) {
            return null;
        }
        final List<String> it = StringUtils.split(ipv4Str, '.', 4);
        if (it.size() != 4) {
            return null;
        }
        final byte[] byteAddress = new byte[4];
        for (int i = 0; i < 4; ++i) {
            final int tempInt = Integer.parseInt(it.get(i));
            if (tempInt > 255) {
                return null;
            }
            byteAddress[i] = (byte)tempInt;
        }
        return byteAddress;
    }
}
